﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 类名或功能模块简述
    /// </summary>
    /// <remarks>
    /// 功能：描述类或者模块的功能，多个功能要用编号分别罗列。
    /// 创建人：张虎
    /// 创建日期：2024/5/22 14:14:13
    /// 修改信息（1）：张虎，2024/5/22 14:14:13，修改内容
    /// </remarks>
    /// 
    [Description("审核是判断单据体的含税单价为空")]
    [HotUpdate]
    public class TransferApplyAuditService: AbstractOperationServicePlugIn
    {
        //初始化任务
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("FBillNo");
            e.FieldKeys.Add("FID");
            //内部业务类型
            e.FieldKeys.Add("F_QLTK_Combo_tzk");
            //调拨类型
            e.FieldKeys.Add("FTRANSTYPE");
            //含税单价
            e.FieldKeys.Add("F_RDLV_HSDJ");
            //含税金额
            e.FieldKeys.Add("F_QLTK_Amount_qtr");
            //税率
            e.FieldKeys.Add("F_QLTK_SL");
            //申请数量
            e.FieldKeys.Add("FQty");
            //调出组织
            e.FieldKeys.Add("FStockOrgId");
        }

        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            base.BeforeExecuteOperationTransaction(e);
            foreach (ExtendedDataEntity extended in e.SelectedRows)
            {
                DynamicObject dy = extended.DataEntity;
                string F_QLTK_Combo_tzk = Convert.ToString(dy["F_QLTK_Combo_tzk"]);//内部业务状态                    
                string FTRANSTYPE = Convert.ToString(dy["TRANSTYPE"]);//调拨类型                   
                DynamicObjectCollection FEntity = extended["STK_STKTRANSFERAPPENTRY"] as DynamicObjectCollection;//明细信息              
                
                if (FTRANSTYPE.Equals("OverOrgTransfer"))
                {
                    foreach (var entryRow in FEntity)
                    {
                        string FEntryId = entryRow["ID"].ToString();
                        string sql = string.Format(@"select * from T_STK_STKTRANSFERAPPENTRY where FEntryId ='" + FEntryId + "'");
                        DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                        if (sql_toStirng.Rows.Count > 0)
                        {
                            double F_RDLV_HSDJ = double.Parse(sql_toStirng.Rows[0]["F_RDLV_HSDJ"].ToString());//含税单价
                            if (F_RDLV_HSDJ == 0)
                            {
                                e.Cancel = true;
                                e.CancelMessage = string.Format("{0}时第{1}行含税单价为空，请填写", this.FormOperation.OperationName, entryRow["Seq"].ToString());
                            }
                        }
                    }
                }

            }
        }
    }
}
